#include "picture.h"
#include "ui_picture.h"
#include "teacher.h"
#include "student.h"
#include "program.h"
#include "searchwin.h"
#include "paper.h"
#include <vector>
#include <QPainter>
#include <math.h>
#include <QDebug>
extern vector <Teacher*> tdata;
extern vector <student*> sdata;
extern vector <Program*> prdata;
extern vector <paper*> pdata;
picture::picture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::picture)
{
    ui->setupUi(this);
}
void picture::paintEvent(QPaintEvent *event){
    int n = tdata.size()+sdata.size();
    int tnum = tdata.size();
    int snum = sdata.size();
    QString id1;
    QString id2;
    QPainter painter1(this);
    QPen pen;
    pen.setStyle(Qt::DashDotLine);
    QBrush brush(Qt::cyan);
    brush.setStyle(Qt::Dense6Pattern);
    this->setFixedSize(800,800);
    painter1.setBrush(brush);
    int a[n][2];
    for(int i=0;i<n;i++){
        a[i][0] = 400+350*cos(2*3.1415926/n*(i));
        a[i][1] = 400+350*sin(2*3.1415926/n*(i));
    }
    for(int j=0;j<tnum;j++)
    {/*tdata[j]->drawpic(a[j][0]-25,a[j][1]-10,50,20,this);*/
    painter1.drawRect(QRect(a[j][0]-25,a[j][1]-10,50,20));
    painter1.drawText(QRect(a[j][0]-20,a[j][1]-7,50,20),tdata[j]->findname());
    }
    for(int k=0;k<snum;k++)
    {/*sdata[k]->drawpic(a[tnum+k][0]-25,a[tnum+k][1]-10,50,20,this);*/
    painter1.drawRect(QRect(a[tnum+k][0]-25,a[tnum+k][1]-10,50,20));
    painter1.drawText(QRect(a[tnum+k][0]-20,a[tnum+k][1]-7,50,20),sdata[k]->findname());
    }
    qDebug()<<n;
    for(int i=0;i<=n-2;i++){
        for(int j=i+1;j<=n-1;j++){
            if(i<=tnum-1&&j<=tnum-1){///两个都是老师
               id1 = tdata[i]->findname();
               id2 = tdata[j]->findname();
               for(unsigned int n=0;n<prdata.size();n++){
                   if(prdata[n]->iftogether(id1,id2)){
                       pen.setColor(QColor(247,46,5));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }
               for(unsigned int n=0;n<pdata.size();n++){
                   if(pdata[n]->ifrelation(id1,id2)){
                       pen.setColor(QColor(7,46,205));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }

            }
            if(i<=tnum-1&&j>tnum-1){ ///一是老师，②是学生
               id1 = tdata[i]->findname();
               id2 = sdata[j-tnum]->findname();
               for(unsigned int n=0;n<prdata.size();n++){
                   if(prdata[n]->iftogether(id1,id2)){
                       pen.setColor(QColor(247,46,5));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }
               for(unsigned int n=0;n<pdata.size();n++){
                   if(pdata[n]->ifrelation(id1,id2)){
                       pen.setColor(QColor(7,46,205));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }

            }
            if(i>tnum-1&&j<=tnum-1){ ///一是学生，②是老师
                id1 = sdata[i-tnum]->findname();
                id2 = tdata[j]->findname();
               for(unsigned int n=0;n<prdata.size();n++){
                   if(prdata[n]->iftogether(id1,id2)){
                       pen.setColor(QColor(247,46,5));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }
               for(unsigned int n=0;n<pdata.size();n++){
                   if(pdata[n]->ifrelation(id1,id2)){
                       pen.setColor(QColor(7,46,205));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }

            }
            if(i>tnum-1&&j>tnum-1){ ///1  2是学生，

                id1 = sdata[i-tnum]->findname();
                id2 = sdata[j-tnum]->findname();
//                qDebug()<<id1<<" "<<id2;
               for(unsigned int n=0;n<prdata.size();n++){
                   qDebug()<<prdata[n]->iftogether(id1,id2);
                   if(prdata[n]->iftogether(id1,id2)){
                       pen.setColor(QColor(247,46,5));
                       painter1.setPen(pen);
                       qDebug()<<"dao了";
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                       break;
                   }
               }
               for(unsigned int n=0;n<pdata.size();n++){
                   if(pdata[n]->ifrelation(id1,id2)){
                       pen.setColor(QColor(7,46,205));
                       painter1.setPen(pen);
                       painter1.drawLine(a[i][0],a[i][1],a[j][0],a[j][1]);
                       qDebug()<<"画了";
                     break;
                   }
               }

            }

        }
    }
}

picture::~picture()
{
    delete ui;
}

void picture::on_pushButton_clicked()
{
    this->close();
        searchwin * p = new searchwin;
        p->show();
}
